package com.thy.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Excel导出工具类
 */
public class ExcelExportUtil {

    /**
     * 导出Excel文件
     *
     * @param response  HttpServletResponse对象
     * @param fileName  导出文件名
     * @param sheetName 工作表名称
     * @param clazz     数据模型类
     * @param data      导出数据列表
     * @throws IOException IO异常
     */
    public static void exportExcel(HttpServletResponse response, String fileName, String sheetName,
                                   Class<?> clazz, List<?> data) throws IOException {
        // 设置响应头信息
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 处理文件名中文乱码问题
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        // 获取样式策略
        HorizontalCellStyleStrategy styleStrategy = getStyleStrategy();

        // 写入Excel数据
        EasyExcel.write(response.getOutputStream(), clazz)
                .sheet(sheetName)
                .registerWriteHandler(styleStrategy)
                .doWrite(data);
    }

    /**
     * 自定义Excel样式
     *
     * @return 样式策略对象
     */
    private static HorizontalCellStyleStrategy getStyleStrategy() {
        // 表头样式设置
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 设置表头背景色为浅蓝色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

        // 表头字体设置
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12); // 字体大小
        headWriteFont.setBold(true); // 加粗
        headWriteFont.setFontName("微软雅黑"); // 字体
        headWriteCellStyle.setWriteFont(headWriteFont);

        // 表头对齐方式
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); // 水平居中
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        // 设置表头边框
        setBorder(headWriteCellStyle);

        // 内容样式设置
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 内容字体设置
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 11);
        contentWriteFont.setFontName("微软雅黑");
        contentWriteCellStyle.setWriteFont(contentWriteFont);

        // 内容对齐方式
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT); // 水平左对齐
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中

        // 设置内容边框
        setBorder(contentWriteCellStyle);

        // 创建并返回样式策略对象
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }

    /**
     * 为单元格设置边框
     *
     * @param writeCellStyle 单元格样式对象
     */
    private static void setBorder(WriteCellStyle writeCellStyle) {
        writeCellStyle.setBorderTop(BorderStyle.THIN);
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
    }
}

